package com.wootide.cms.common.manager;

import net.sf.json.JSONObject;

/**
 *	@author 
 *  @date 
 */
public class ResultMgr {
	
	private static String  BASE_RESULT = "BaseResult";
	
	private static String  OL_RESULT = "OlResult";
	
	private static String  LIST_RESULT = "ListResult";
	
	private static String  OBJECT_RESULT = "ObjectResult";
	
	private static String LINE_CHART_RESULT = "LineChartResult";
	
	/**
	 *  BaseResult
	 *  ListResult
	 *  ObjectResult
	 *  
	 */
	private String rtType;
	
	private BaseResult result;

	
	private static String  XML = "xml";
	
	private static String  JSON = "json";
	
	private String adpType = JSON;

	public ResultMgr(){
		
	}
	
	public ResultMgr(BaseResult br,String type){
		this.result = br;
		this.rtType = br.getClass().getSimpleName();
		if(null != type && !"".equals(type)){
			this.adpType = type;
		}
	};
	
	
	
	public String convertResult(BaseResult br,String type){
		this.result = br;
		this.rtType = br.getClass().getSimpleName();
		if(null != type && !"".equals(type)){
			this.adpType = type;
		}
		return convertResult();
	};
	
	public String convertResult(){
		
		if(XML.equals(this.adpType)){
			
		}else if(JSON.equals(this.adpType)){
			return resultToJson();
		}
		return "";
	};

	
	
	public String resultToJson(BaseResult br){
		this.result = br;
		this.rtType = br.getClass().getSimpleName();
		return resultToJson();
	}
	
	
	public String resultToJson(){
		
		if(null == result){
			return "";
		}
		
		if( BASE_RESULT.equals(rtType)){
			
			return brToJson();
		}else if(OL_RESULT.equals(rtType)){
			
			return olrToJson();
		}else if(LIST_RESULT.equals(rtType)){
			
			return lrToJson();
		}else if(OBJECT_RESULT.equals(rtType)){
			
			return orToJson();
		}
//		else if(TREE_RESULT.equals(rtType)){
//			
//			return trToJson();
//		}
		else if(LINE_CHART_RESULT.equals(rtType)){
			
			return lcrToJson();
		}
		
		return "";
	};
	
	private String brToJson(){
		
		JSONObject json = JSONObject.fromObject(result);

		return json.toString();
	};
	
	private String lrToJson(){
		
		JSONObject json = JSONObject.fromObject(result);

		return json.toString();
	};
	
	private String orToJson(){
		
		JSONObject json = JSONObject.fromObject(result);

		return json.toString();
	};
	
	private String olrToJson(){
		
		JSONObject json = JSONObject.fromObject(result);

		return json.toString();
	};
	
	
//	/** 把TreeResult转换成Json字符串 */
//	private String trToJson(){
//		
//
//		
//		TreeResult tr = (TreeResult)result;
//		
//		JSONArray json = JSONArray.fromObject(tr.getItems());
//		
////		JSONObject json = JSONObject.fromObject(result);
//
//		return json.toString();
//	};
	
	private String lcrToJson(){
		
//		LineChartResult lcr = (LineChartResult)result;
		
		JSONObject json = JSONObject.fromObject(result);

		return json.toString();
	}
	
	
	
}
